﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using Caliburn.Micro;
using Wulong.Phone.Dianping.Helpers;
using Wulong.Phone.Dianping.Models;
using Wulong.Phone.Dianping.Services;
using Wulong.Phone.Dianping.Utils;

namespace Wulong.Phone.Dianping.ViewModels
{
    public class MerchantsByChannelViewModel : 
        PaginationViewModelBase
    {
        private readonly INavigationService _navigation;
        private IDisposable _disposable;
        public MerchantsByChannelViewModel(
            INavigationService navigation)
        {
            _navigation = navigation;
            _merchants = new ObservableCollection<Merchant>();
            if (Page < 1)
                Page = 1;
        }

        protected override void OnViewReady(object view)
        {
            base.OnViewReady(view);
            if(_merchants.Count()==0)
                Load();
            SelectedItem = null;
        }

        private bool _canLoad;
        public bool CanLoad
        {
            get { return _canLoad; }
            set
            {
                _canLoad = value;
                NotifyOfPropertyChange(() => CanLoad);
            }
        }

        public void Load()
        {
            IsBusy = true;
            CanLoad = false;
            CanLoadMore = false;
            Page = 1;
            _merchants.Clear();
            _disposable=DianpingService.MerchantsByChannel(
                (ChannelEnum)ChannelId, 
                CityCode)
                .ObserveOnDispatcher()
                .Finally(() =>
                             {
                                 IsBusy = false;
                                 CanLoad = true;
                             })
                .Subscribe(item =>
                    _merchants.Add(item),
                    error => MessageHelper.ShowErrorMessage(),
                    () =>
                    {
                        CanLoadMore = _merchants.Count >0;
                    }
                );
        }

// ReSharper disable InconsistentNaming
        internal ObservableCollection<Merchant> _merchants;
// ReSharper restore InconsistentNaming
        
        public IEnumerable<Merchant> Merchants
        {
            get { return _merchants; }
        }

        public int ChannelId { get; set; }
        private string _channelName;
        public string ChannelName
        {
            get { return _channelName; } 
            set
            {
                _channelName = value;
                NotifyOfPropertyChange(()=>ChannelName);
            }
        }

        private Merchant _selectedItem;
        public Merchant SelectedItem
        {
            get { return _selectedItem; }
            set 
            { 
                _selectedItem = value;
                NotifyOfPropertyChange(()=>SelectedItem);
            }
        }

        public void LoadMore()
        {
            IsBusy = true;
            CanLoad = false;
            _disposable=DianpingService.MerchantsByChannel(
                (ChannelEnum)ChannelId,
                CityCode,
                ++Page)
                .ObserveOnDispatcher()
                .Finally(() =>
                             {
                                 IsBusy = false;
                                 CanLoad = true;
                             })
                .Subscribe(
                item => _merchants.Add(item),
                    error =>
                    {
                        MessageHelper.ShowErrorMessage();
                        --Page;
                    },
                    () =>
                    {
                        CanLoadMore = _merchants.Count > 0;
                    }
                );
        }

        public void MerchantDetail()
        {
            if(SelectedItem==null) return;
            _navigation
                .UriFor<MerchantPivotViewModel>()
                .WithParam(x=>x.CityCode, CityCode)
                .WithParam(x=>x.MerchantId, SelectedItem.Id)
                .WithParam(x=>x.ImageUrl, SelectedItem.ImageUrl)
                .WithParam(x=>x.MerchantAvgPrice, SelectedItem.AvgPrice)
                .WithParam(x=>x.MerchantOverallRating, SelectedItem.OverallRating)
                .Navigate();
        }

        public IEnumerable<IResult> LaunchChannelDialog()
        {
            var vm = new ShowDialog<ChannelDialogViewModel>()
                .Init(x => x.CityCode=CityCode);
            yield return vm;
            if (vm.Dialog.ChannelId !=0)
            {
                ChannelId = vm.Dialog.ChannelId;
                ChannelName = vm.Dialog.ChannelName;
                Load();
            }
        }

        protected override void OnDeactivate(bool close)
        {
            base.OnDeactivate(close);
            if(_disposable!=null)
                _disposable.Dispose();
        }

        public void Search()
        {
            _navigation.UriFor<SearchViewModel>()
                .WithParam(x => x.CityCode, CityCode)
                .Navigate();
        }

        public void GoToAboutPage()
        {
            _navigation.UriFor<AboutViewModel>()
                .Navigate();
        }
    }
}
